package com.gxa.admin.controller;

import com.gxa.admin.pojo.dto.UserDto;
import com.gxa.admin.pojo.vo.UserVo;
import com.gxa.admin.service.UserService;
import com.gxa.admin.utils.QueryResult;
import com.gxa.admin.utils.ValidatorUtils;
import com.gxa.core.result.MessageCode;
import com.gxa.core.result.Result;
import com.gxa.core.result.ResultUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author lch
 * @date 2024/3/21 9:46
 * @description 用户信息
 **/

@RestController
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 查询所有用户信息
     * @param userDto
     * @return
     */
    @GetMapping("/admin/VipManger/selectVipMangerByPage")
    public Result getAllUser(UserDto userDto){

        QueryResult queryResult = this.userService.getAllUser(userDto);

        if (queryResult != null) {
            //查询成功
            return ResultUtils.buildSuccess(queryResult.getCount(), queryResult.getData());
        } else {
            //查询失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }


    /**
     * 添加用户信息
     * @param userDto
     * @return
     */
    @PostMapping("/admin/VipManger/add")
    public Result addUser(@RequestBody UserDto userDto) {

        //数据校验
        ValidatorUtils.validateEntity(userDto);

        int result = this.userService.addUser(userDto);
        if (result > 0) {
            //添加成功
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        } else {
            //添加失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }


    /**
     * 更新会员等级
     * @param userDto
     * @return
     */
    @PostMapping("/admin/VipManger/updateVipInfo")
    public Result updateVipLevel(@RequestBody UserDto userDto) {

        //数据校验
        ValidatorUtils.validateEntity(userDto);

        int result = this.userService.updateUser(userDto);
        if (result > 0) {
            //更新成功
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        } else {
            //更新失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }


    /**
     * 根据id删除会员等级
     * @param userId
     * @return
     */
    @GetMapping("/admin/VipManger/deleteVipManger")
    public Result deleteVipLevel(Integer userId){
        int result = this.userService.deleteUser(userId);
        if (result > 0) {
            //删除成功
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        } else {
            //删除失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }


    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     */
    @GetMapping("/admin/VipManger/selectVipInfoById")
    public Result getUserById(Integer userId){
        UserVo user = this.userService.getUserById(userId);
        if (user!=null){
            //查询成功
            return ResultUtils.buildSuccess(user);
        }else {
            //查询失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }

}
